<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot-scope="{ row }" slot="menu">
        <el-button
          type="text"
          icon="el-icon-view"
          size="small"
          plain
          style="border: 0; background-color: transparent !important"
          v-if="permission.salesReturn_view"
          @click.stop="handleView(row)"
          >详情
        </el-button>
      </template>
    </avue-crud>
    <el-dialog append-to-body :close-on-click-modal="false" title="详情" custom-class="salesReturnDialog" :visible.sync="showDialog" v-if="showDialog" width="60%" top="7vh">
      <div class="common-modal-sub-title">
        <i class="el-icon-vertical-bar avue-group__icon"></i>
        单据信息
      </div>
      <avue-form v-if="showDialog" ref="properties" v-model="properties" :option="propertiesOption"> </avue-form>
      <div class="common-modal-sub-title">
        <i class="el-icon-vertical-bar avue-group__icon"></i>
        单据详情
      </div>
      <avue-crud
        :option="sheetOption"
        :table-loading="detailLoading"
        :data="sheetData"
        :permission="permissionList"
        :before-open="beforeOpen"
        v-model="sheetForm"
        ref="addTableCrud"
      >
      </avue-crud>
      <div slot="footer" class="dialog-footer close">
        <el-button size="small" icon="el-icon-circle-close" @click="showDialog = false">取 消</el-button>
      </div>
    </el-dialog>
  </basic-container>
</template>

<script>
import { getList, getDetail } from '@/api/warehouse/sales-return';
import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config/index.vue';
import userConfig from '@/mixins/userConfig';
export default {
  components: {
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'SALES_RETURN',
      form: {},
      typeObj: {},
      sheetForm: {},
      properties: {},
      loading: false,
      detailLoading: false,
      tableOption: [], //模态框配置
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      showDialog: false,
      data: [],
      sheetData: [],
      replacePartData: [],
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuPosition: 'right',
        searchMenuSpan: 6,
        dialogClickModal: false,
        border: true,
        height: 'calc(100vh - 360px)',
        viewBtn: false,
        addBtn: false,
        selection: false,
        printBtn: false,
        editBtn: false,
        delBtn: false,
        index: true,
        indexLabel: '序号',
        searchSpan: 5,
        searchLabelWidth: 80,
        columnBtn: false,
        menuWidth: 100,
        column: [
          {
            label: '退货单号',
            prop: 'returnOrderNo',
            searchSpan: 6,
            search: true
          },
          {
            label: '客户编码',
            prop: 'customerCode',
            searchSpan: 6,
            search: true
          },
          {
            label: '客户名称',
            prop: 'customerName',
            searchSpan: 6,
            search: true
          }
        ]
      },
      propertiesOption: {
        //单据FORM配置
        labelWidth: '110',
        submitBtn: false,
        emptyBtn: false,
        detail: true,
        column: [
          {
            label: '退货单号',
            prop: 'returnOrderNo',
            span: 8
          },
          {
            label: '客户编码',
            prop: 'customerCode',
            span: 8
          },
          {
            label: '客户名称',
            prop: 'customerName',
            span: 8
          }
        ]
      },
      sheetOption: {
        tip: false,
        searchShow: true,
        searchSpan: 4,
        searchMenuSpan: 6,
        dialogClickModal: false,
        height: '400',
        border: true,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        cancelBtn: false,
        saveBtn: false,
        selection: false,
        menu: false,
        page: false,
        index: true,
        indexLabel: '序号',
        refreshBtn: false,
        expandBtn: false,
        columnBtn: false,
        column: [
          {
            label: '行号',
            prop: 'returnOrderLot'
          },
          {
            label: '产品编码',
            prop: 'productPartNo'
          },
          {
            label: '产品名称',
            prop: 'productPartName'
          },
          {
            label: '产品型号',
            prop: 'productPartModel'
          },
          {
            label: '出库数量',
            prop: 'outboundQuantity'
          },
          {
            label: '计划退货数量',
            prop: 'planReturnQuantity'
          },
          {
            label: '实际退货数量',
            prop: 'actualReturnQuantity'
          }
        ]
      }
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.salesReturn_add, false),
        viewBtn: this.vaildData(this.permission.salesReturn_view, false),
        delBtn: this.vaildData(this.permission.salesReturn_delete, false),
        editBtn: this.vaildData(this.permission.salesReturn_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  methods: {
    beforeOpen(done, type) {
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },

    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query))
        .then((res) => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.selectionClear();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /**
     * 查看模态框
     */
    handleView(row) {
      this.getSalesReturnDetail(row.id);
      this.showDialog = true;
    },
    /**
     * 获取单据详情
     */
    getSalesReturnDetail(id) {
      this.detailLoading = true;
      getDetail(id)
        .then((res) => {
          const data = res.data.data;
          this.sheetData = data.returnOrderDetailBaseInfo;
          this.properties = structuredClone(data.returnOrderBaseInfo);
        })
        .finally(() => {
          this.detailLoading = false;
        });
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .salesReturnDialog {
  .el-dialog__body {
    padding: 0 20px 20px;
  }
  .common-modal-sub-title {
    display: flex;
    align-items: center;
    font-size: 16px;
    font-weight: bold;
    color: rgba(0, 0, 0, 0.85);
    margin: 10px 0;
  }
  .no-print {
    display: none;
  }
  .avue-crud__menu {
    display: none;
  }
}
</style>
